/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package units;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class Sample implements Comparable{

    private MonkeyType monkeyType;
    private CellType cellType;
    private int id;
    private String sampleName;

    public static List<Sample> getSamples(Handle h){
        List<Sample> samples = new ArrayList<Sample>();
        String sql = "select * from monkeys.sample";
        ResultSet rs = h.runSQLquery(sql);
        try {
            while (rs.next()) {
                Sample sample = new Sample();
                sample.setId(rs.getInt("id"));
                sample.setSampleName(rs.getString("sampleName"));
                sample.setMonkeyType(MonkeyType.valueOf(rs.getString("MonkeyType")));
                sample.setCellType(CellType.valueOf(rs.getString("cellType")));
                samples.add(sample);
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }
        return samples;
    }

    @Override
    public boolean equals(Object o){
        Sample other = (Sample) o;
        return this.getId()==other.getId();
    }


    public CellType getCellType() {
        return cellType;
    }

    public void setCellType(CellType cellType) {
        this.cellType = cellType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public MonkeyType getMonkeyType() {
        return monkeyType;
    }

    public void setMonkeyType(MonkeyType monkeyType) {
        this.monkeyType = monkeyType;
    }

    public String getSampleName() {
        return sampleName;
    }

    public void setSampleName(String sampleName) {
        this.sampleName = sampleName;
    }

    public int compareTo(Object o) {
        Sample other = (Sample) o;
        return new Integer(this.getId()).compareTo(new Integer(other.getId()));
    }

    public List<Integer> getTimePoints(){
        Handle h = Handle.getConnectedHandle("monkeys");
        String sql = "select distinct timePoint from expression where sampleId="+this.getId();
        ResultSet rs = h.runSQLquery(sql);
        List<Integer> timePoints = new ArrayList<Integer>();
        try {
            while(rs.next()){
                timePoints.add(rs.getInt("timePoint"));
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return timePoints;
    }


    

}
